package org.example.maibobo.processor;

import org.example.maibobo.decode.InvokeFunc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.StateMachineEnum;
import org.smartboot.socket.transport.AioSession;

import java.io.IOException;


public class MaiboboMessageProcessor implements MessageProcessor<byte[]> {

    private static final Logger LOGGER = LoggerFactory.getLogger(MaiboboMessageProcessor.class);

    @Override
    public void process(AioSession session, byte[] data) {
        // 从网络接收到的数据
        try {
            LOGGER.debug("协议数据长度 {}", data.length);
            if (data.length == 31) {
                InvokeFunc.authentication(session, data);
            }

            if (data.length == 37) {
                InvokeFunc.currentMeasurementData(session, data);
            }

            // 设备上传记忆值
            if (data.length == 128) {
                InvokeFunc.earlyMemoryData(session, data);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public void stateEvent(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) {
        MessageProcessor.super.stateEvent(session, stateMachineEnum, throwable);
        //初次链接
        if (stateMachineEnum == StateMachineEnum.NEW_SESSION) {
            LOGGER.info("maibobo 已链接！");
        }
    }
}
